# _*_ encoding:utf-8 _*_
from django.utils import timezone
from datetime import timedelta

from rest_framework.views import APIView
from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from rest_framework.permissions import IsAdminUser

from users.models import User
from orders.models import OrderInfo
from goods.models import GoodsVisitCount
from meiduo_admin.serializers.home_serializers import GoodsVisitCountModelSerializer


class UserTotalCountView(APIView):
    permission_classes = [IsAdminUser]  # 当前视图只有is_staff=True才能访问

    def get(self, request):

        count = User.objects.count()

        cur_time = timezone.localtime()
        print('cur_time：', cur_time)

        return Response({
            'count': count,
            'date': cur_time.date()
        })


class UserDayCountView(APIView):
    permission_classes = [IsAdminUser]

    def get(self, request):

        cur_0_time = timezone.localtime().replace(hour=0, minute=0, second=0)
        print('日增用户：', cur_0_time)

        count = User.objects.filter(date_joined__gte=cur_0_time).count()

        return Response({
            'count': count,
            'date': cur_0_time.date()
        })


class UserActiveCountView(APIView):
    permission_classes = [IsAdminUser]

    def get(self, request):

        cur_0_time = timezone.localtime().replace(hour=0, minute=0, second=0)

        count = User.objects.filter(last_login__gte=cur_0_time).count()

        return Response({
            'count': count,
            'date': cur_0_time.date()
        })


class UserOrderCountView(APIView):
    permission_classes = [IsAdminUser]

    def get(self, request):

        cur_0_time = timezone.localtime().replace(hour=0, minute=0, second=0)

        orders = OrderInfo.objects.filter(create_time__gte=cur_0_time)

        users = set(order.user for order in orders)

        return Response({
            'count': len(users),
            'date': cur_0_time.date()
        })


class UserMonthCountView(APIView):
    permission_classes = [IsAdminUser]

    def get(self, request):

        # 今日时间
        cur_0_time_now = timezone.localtime().replace(hour=0, minute=0, second=0)

        # 30天前
        cur_0_time_30 = cur_0_time_now - timedelta(days=29)

        count_list = []
        for i in range(30):

            # 当天时间，0点
            cur_date = cur_0_time_30 + timedelta(days=i)

            # 次日时间，0点
            next_date = cur_date + timedelta(days=1)

            count = User.objects.filter(date_joined__gte=cur_date, date_joined__lt=next_date).count()
            count_list.append({
                'count': count,
                'date': cur_date.date()
            })

        return Response(count_list)


class GoodsDayView(ListAPIView):
    permission_classes = [IsAdminUser]

    queryset = GoodsVisitCount.objects.all()
    serializer_class = GoodsVisitCountModelSerializer

    def get_queryset(self):

        cur_0_time_now = timezone.localtime().replace(hour=0, minute=0, second=0)

        return self.queryset.filter(create_time__gte=cur_0_time_now)



